from csv_persister import CsvPersister
from pycommons import AbstractObject


class GenerateId(object):
    def __init__(self, csv_path='/tmp', csv_file='/generate_id.csv'):
        self.persister = CsvPersister(csv_path + csv_file, use_id=False)

    def generate_id(self,section='default'):
        new_id = 1
        for item in self.persister.get_list(UniqueId):
            if item.section == section:
                new_id = item.generate_id + 1
# TODO:               self.persister.remove_by_field(item, 'section',section)

        new_item = UniqueId()
        new_item.section = section
        new_item.generate_id = new_id
        self.persister.add(new_item)
        return new_id
    
def generate_tmp_id():
    return str(id(UniqueId()))
    
class UniqueId(AbstractObject):
    def get_fields(self):
        return [\
                  ['section', 'Section', str]
                 ,['generate_id','Id',int]
               ]   